package com.hongning.railway.ui.main.mine.partymember;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.QueryCpInfoRequestBean;
import com.hongning.railway.entity.reqbean.QuestionResultBean;

import com.hongning.railway.entity.reqbean.ReqAssessesIntegralBean;
import com.hongning.railway.ui.main.cadremanager.CadreResultFragment;
import com.hongning.railway.ui.main.cadremanager.itemviewmodel.CadreGradeItemViewModel;
import com.hongning.railway.ui.main.mine.partymember.itemviewmodel.PartyQuestionItemNameViewModel;
import com.hongning.railway.ui.main.mine.partymember.itemviewmodel.QuestionItemViewModel;
import com.hongning.railway.ui.uichangeobserable.DialogUIChangeObserable;
import com.hongning.railway.ui.uichangeobserable.ListUIChangeObserable;
import com.hongning.railway.util.Apputils;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class PartyQuestionFragmentViewModel extends BaseViewModel<AppRepository> {
    public ItemBinding<PartyQuestionItemNameViewModel> item = ItemBinding.of(BR.viewModel, R.layout.item_partquestion_name);
    public ObservableList<PartyQuestionItemNameViewModel> viewlist = new ObservableArrayList<>();
    public ObservableField<Boolean> inputfilter = new ObservableField<>(true);
    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<ReqAssessesIntegralBean> reqAssessesIntegralBeanObservableField = new ObservableField<>();
    public DialogUIChangeObserable dialogUIChangeObserable = new DialogUIChangeObserable();

    public ObservableField<String> status = new ObservableField<>();
    public BindingCommand submitCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            status.set("submit");
            if (reqAssessesIntegralBeanObservableField.get().getSelfEvaluationIntegral()==null|| TextUtils.isEmpty(reqAssessesIntegralBeanObservableField.get().getSelfEvaluationIntegral())){
                dialogUIChangeObserable.dialogshow.setValue("当前未对自己评价，确定提交当前测评结果？");
                return;
            }
            for (ReqAssessesIntegralBean.BeevaluatedBean beevaluatedBean:reqAssessesIntegralBeanObservableField.get().getBeevaluated())
            {
                if (beevaluatedBean.getMutualEvaluationIntegral()==null||TextUtils.isEmpty(beevaluatedBean.getMutualEvaluationIntegral()))
                {
                    dialogUIChangeObserable.dialogshow.setValue("当前有未评价的党员，确定提交当前测评结果？");
                    return;
                }
            }
            submit();
        }
    });


    public BindingCommand saveCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            status.set("save");
            if (reqAssessesIntegralBeanObservableField.get().getSelfEvaluationIntegral()==null|| TextUtils.isEmpty(reqAssessesIntegralBeanObservableField.get().getSelfEvaluationIntegral())){
                dialogUIChangeObserable.dialogshow.setValue("当前未对自己评价，确定保存当前测评结果？");
                return;
            }
            for (ReqAssessesIntegralBean.BeevaluatedBean beevaluatedBean:reqAssessesIntegralBeanObservableField.get().getBeevaluated())
            {
                if (beevaluatedBean.getMutualEvaluationIntegral()==null||TextUtils.isEmpty(beevaluatedBean.getMutualEvaluationIntegral()))
                {
                    dialogUIChangeObserable.dialogshow.setValue("当前有未评价的党员，确定保存当前测评结果？");
                    return;
                }
            }
            save();
        }
    });

    public PartyQuestionFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
        name.set(Apputils.getLoginInfo().getUserName());
    }

    /**
     * 保存
     */
    public void save() {
        model.saveAssessesIntegral(reqAssessesIntegralBeanObservableField.get())
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).doOnTerminate(new Action() {
            @Override
            public void run() throws Exception {
                dismissDialog();
            }
        }).subscribe(new ApiDisposableObserver() {
            @Override
            public void onResult(Object o) {
                Log.i("***************",o.toString());
                ToastUtils.showShort("保存成功");

            }
        });
    }
    /**
     * 提交
     */
    public void submit() {
        model.addAssessesIntegral(reqAssessesIntegralBeanObservableField.get())
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).doOnTerminate(new Action() {
            @Override
            public void run() throws Exception {
                dismissDialog();
            }
        }).subscribe(new ApiDisposableObserver() {
            @Override
            public void onResult(Object o) {
                Log.i("***************",o.toString());
                startContainerActivity(CadreResultFragment.class.getCanonicalName());
                finish();
            }
        });
    }

    /**
     * 获取测评数据人员列表
     * @param questionResultBean
     */
    public void getCpDatas(QuestionResultBean questionResultBean){
        QueryCpInfoRequestBean queryCpInfoRequestBean =new QueryCpInfoRequestBean(questionResultBean.getEvaluationId(),Apputils.getUserId());

        model.selectAssessesIntegralById(queryCpInfoRequestBean)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).doOnTerminate(new Action() {
            @Override
            public void run() throws Exception {
                dismissDialog();
            }
        }).subscribe(new ApiDisposableObserver<ReqAssessesIntegralBean>() {
            @Override
            public void onResult(ReqAssessesIntegralBean questionResultBeans) {
                if (questionResultBeans==null||questionResultBeans.getBeevaluated()==null||questionResultBeans.getBeevaluated().isEmpty())
                {
                    ToastUtils.showShort("没有互评人员");
                    return;
                }
                reqAssessesIntegralBeanObservableField.set(questionResultBeans);
                for (ReqAssessesIntegralBean.BeevaluatedBean beevaluatedBean:questionResultBeans.getBeevaluated())
                {
                    viewlist.add(new PartyQuestionItemNameViewModel(PartyQuestionFragmentViewModel.this,beevaluatedBean));
                }

            }
        });
    }
}
